#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Desc: 基于生成器的上下文管理

from contextlib import contextmanager


@contextmanager
def file_manager(fp, mode):
    try:
        f = open(fp, mode)
        yield f
    finally:
        f.close()

with file_manager('test.txt', 'w') as f:
    f.write('hello by `file_managwe`')
